<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="alternate" href="/atom.xml" title="el's blog" type="application/atom+xml">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '7.5.0',
    exturl: false,
    sidebar: {"position":"right","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="研读ES6标准，正好碰到写Java的同事对于 Js 中逻辑与的疑惑。">
<meta name="keywords" content="ES6">
<meta property="og:type" content="article">
<meta property="og:title" content="ES6标准之逻辑与的规则">
<meta property="og:url" content="http:&#x2F;&#x2F;ever-lose.gitee.io&#x2F;2017&#x2F;12&#x2F;16&#x2F;javascript&#x2F;ES6-standard-LogicalANDExpression&#x2F;index.html">
<meta property="og:site_name" content="el&#39;s blog">
<meta property="og:description" content="研读ES6标准，正好碰到写Java的同事对于 Js 中逻辑与的疑惑。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-11-08T06:19:30.886Z">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://ever-lose.gitee.io/2017/12/16/javascript/ES6-standard-LogicalANDExpression/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>ES6标准之逻辑与的规则 | el's blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">el's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">everlose's blog</h1>
      
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ever-lose.gitee.io/2017/12/16/javascript/ES6-standard-LogicalANDExpression/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpeg">
      <meta itemprop="name" content="everlose">
      <meta itemprop="description" content="focus on Js、CSS、Node...etc">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="el's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          ES6标准之逻辑与的规则
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2017-12-16 17:49:11" itemprop="dateCreated datePublished" datetime="2017-12-16T17:49:11+08:00">2017-12-16</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-11-08 14:19:30" itemprop="dateModified" datetime="2019-11-08T14:19:30+08:00">2019-11-08</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/javascript/" itemprop="url" rel="index">
                    <span itemprop="name">javascript</span>
                  </a>
                </span>
            </span>

          
            <div class="post-description">研读ES6标准，正好碰到写Java的同事对于 Js 中逻辑与的疑惑。</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>最近看到了阮一峰的一篇(读懂 ECMAScript 规格](<a href="http://www.ruanyifeng.com/blog/2015/11/ecmascript-specification.html)，颇受鼓舞，特此收录一些在ES标准中能给我解惑的知识点。" target="_blank" rel="noopener">http://www.ruanyifeng.com/blog/2015/11/ecmascript-specification.html)，颇受鼓舞，特此收录一些在ES标准中能给我解惑的知识点。</a></p>
<p>恰逢写RN的原生同事有下面的问题相问：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">obj.subObj &amp;&amp; obj.subObj.name <span class="comment">// 如果subObj存在，则表达式会输出name而非输出一个布尔值，为何？</span></span><br></pre></td></tr></table></figure>

<h2 id="解答"><a href="#解答" class="headerlink" title="解答"></a>解答</h2><h3 id="ES6原版文档"><a href="#ES6原版文档" class="headerlink" title="ES6原版文档"></a>ES6原版文档</h3><p><a href="http://www.ecma-international.org/ecma-262/6.0/" target="_blank" rel="noopener">ES6标准在线英文文档地址</a></p>
<blockquote>
<p>12.12.3 Runtime Semantics: Evaluation</p>
</blockquote>
<p>LogicalANDExpression : LogicalANDExpression &amp;&amp; BitwiseORExpression</p>
<ol>
<li>Let lref be the result of evaluating LogicalANDExpression.</li>
<li>Let lval be GetValue(lref).</li>
<li>Let lbool be ToBoolean(lval).</li>
<li>ReturnIfAbrupt(lbool).</li>
<li>If lbool is false, return lval.</li>
<li>Let rref be the result of evaluating BitwiseORExpression.</li>
<li>Return GetValue(rref).</li>
</ol>
<h3 id="翻译"><a href="#翻译" class="headerlink" title="翻译"></a>翻译</h3><ol>
<li>令 lref 为解释执行 LogicalANDExpression 的结果。</li>
<li>令 lval 为 GetValue(lref)。</li>
<li>如果 ToBoolean(lval) 为 false，返回 lval。</li>
<li>令 rref 为解释执行 BitwiseORExpression 的结果。</li>
<li>返回 GetValue(rref)。</li>
</ol>
<p>关键在第三部 ，如果 lref 左表达式执行得到的值 lval 不能被当作 false 的时候直接返回 lval，否则就要返回右表达式的执行值 rref 的具体value，而不是 bool 值。</p>
<h3 id="ToBoolean"><a href="#ToBoolean" class="headerlink" title="ToBoolean"></a>ToBoolean</h3><table>
<thead>
<tr>
<th>Argument Type</th>
<th>Result</th>
</tr>
</thead>
<tbody><tr>
<td>Completion Record</td>
<td>If argument is an abrupt completion, return argument. Otherwise return ToBoolean(argument.[[value]])</td>
</tr>
<tr>
<td>Undefined</td>
<td>Return false.</td>
</tr>
<tr>
<td>Null</td>
<td>Return false.</td>
</tr>
<tr>
<td>Boolean</td>
<td>Return argument.</td>
</tr>
<tr>
<td>Number</td>
<td>Return false if argument is +0, −0, or NaN; otherwise return true.</td>
</tr>
<tr>
<td>String</td>
<td>Return false if argument is the empty String (its length is zero); otherwise return true.</td>
</tr>
<tr>
<td>Symbol</td>
<td>Return true.</td>
</tr>
<tr>
<td>Object</td>
<td>Return true.</td>
</tr>
</tbody></table>
<p>可知左表达式如果为 <code>undefeind</code>、<code>null</code>、<code>false</code>、<code>0</code>、<code>NaN</code>、<code>空字符串</code> 的时候被当作 <code>false</code>。反之为 <code>true</code>，则返回<code>GetValue(rref)</code>。</p>
<h3 id="GetValue"><a href="#GetValue" class="headerlink" title="GetValue"></a>GetValue</h3><ol>
<li>ReturnIfAbrupt(V).</li>
<li>If Type(V) is not Reference, return V.</li>
<li>Let base be GetBase(V).</li>
<li>If IsUnresolvableReference(V), throw a ReferenceError exception.</li>
<li>If IsPropertyReference(V), then<ol>
<li>If HasPrimitiveBase(V) is true, then<ol>
<li>Assert: In this case, base will never be null or undefined.</li>
<li>Let base be ToObject(base).</li>
</ol>
</li>
<li>Return base.<a href="GetReferencedName(V">[Get]</a>, GetThisValue(V)).</li>
</ol>
</li>
<li>Else base must be an Environment Record,<ol>
<li>Return base.GetBindingValue(GetReferencedName(V), IsStrictReference(V)) (see 8.1.1).</li>
</ol>
</li>
</ol>
<p>NOTE： The object that may be created in step 5.a.ii is not accessible outside of the above abstract operation and the ordinary object [[Get]] internal method. An implementation might choose to avoid the actual creation of the object.</p>
<p>第二步谈到如果不是一个引用类型的变量则直接返回这个变量本身，所以 <code>obj.subObj.name</code> 返回的就是 name 属性。</p>
<h2 id="扩展"><a href="#扩展" class="headerlink" title="扩展"></a>扩展</h2><p>那么逻辑或也是同理</p>
<p>LogicalORExpression : LogicalORExpression || LogicalANDExpression</p>
<ol>
<li>Let lref be the result of evaluating LogicalORExpression.</li>
<li>Let lval be GetValue(lref).</li>
<li>Let lbool be ToBoolean(lval).</li>
<li>ReturnIfAbrupt(lbool).</li>
<li>If lbool is true, return lval.</li>
<li>Let rref be the result of evaluating LogicalANDExpression.</li>
<li>Return GetValue(rref).</li>
</ol>
<p>只是判断 lval 能被当成 true 的时候则返回 lval 本身，否则返回 GetValue(rref).</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/ES6/" rel="tag"># ES6</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
                <a href="/2017/12/11/js-util/what-webpack-has-compiled-vue-project/" rel="next" title="webpack 把你的 vue 项目编译成了什么">
                  <i class="fa fa-chevron-left"></i> webpack 把你的 vue 项目编译成了什么
                </a>
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
                <a href="/2018/01/14/mobile-h5/video-black/" rel="prev" title="video 标签加载视频有声音却黑屏">
                  video 标签加载视频有声音却黑屏 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="gitalk-container"></div>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#解答"><span class="nav-number">2.</span> <span class="nav-text">解答</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#ES6原版文档"><span class="nav-number">2.1.</span> <span class="nav-text">ES6原版文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#翻译"><span class="nav-number">2.2.</span> <span class="nav-text">翻译</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ToBoolean"><span class="nav-number">2.3.</span> <span class="nav-text">ToBoolean</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#GetValue"><span class="nav-number">2.4.</span> <span class="nav-text">GetValue</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#扩展"><span class="nav-number">3.</span> <span class="nav-text">扩展</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="site-author-image" itemprop="image" alt="everlose"
    src="/images/avatar.jpeg">
  <p class="site-author-name" itemprop="name">everlose</p>
  <div class="site-description" itemprop="description">focus on Js、CSS、Node...etc</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">97</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">12</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="feed-link motion-element">
    <a href="/atom.xml" rel="alternate">
      <i class="fa fa-rss"></i>RSS
    </a>
  </div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/everlose" title="GitHub &amp;rarr; https:&#x2F;&#x2F;github.com&#x2F;everlose" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/mailto:ever-lose@foxmail.com" title="E-Mail &amp;rarr; mailto:ever-lose@foxmail.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">everlose</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.0.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://mist.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> v7.5.0
  </div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/muse.js"></script>
<script src="/js/next-boot.js"></script>



  




  <script src="/js/local-search.js"></script>













  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID: '48fe7d5cb2ac7e2c7fbf',
      clientSecret: '8f67b574218201d3b3ec6efe69276902e711e655',
      repo: 'everlose-blog',
      owner: 'everlose',
      admin: [''],
      id: 'e9dca2180a348697a7d7770dfb2d914f',
        language: 'zh-CN',
      distractionFreeMode: 'true'
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
</script>

</body>
</html>
